package com.fzrskj.cloud.free.core.tool;


import ms.core.support.MailMsg;
import ms.core.tool.MailTool;
import ms.core.tool.StrTool;
import ms.core.tool.SysTool;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;


/**
 * CMailTool.
 *
 * @author wzj.
 * Created on 2019/11/04 0030.
 */
public class CMailTool {
    private static Logger logger = LogManager.getLogger(MailTool.class);

    public CMailTool() {
    }

    public static boolean sendMail(CMailMsgVo mail) throws Exception {
        boolean ret = false;

        Transport ts = null;
         try {
             Properties props = new Properties();
             props.put("mail.smtp.host", mail.getHost());
             props.put("mail.smtp.connectiontimeout", mail.getConnectTimeout());
             props.put("mail.smtp.timeout", mail.getSendTimeout());
             props.put("mail.transport.protocol", "smtp");
             props.put("mail.smtp.auth", mail.getAuth());
             props.put("mail.smtp.port", mail.getPort());
             props.put("mail.smtp.ssl.enable", mail.getSsl());
             Session session = Session.getDefaultInstance(props, (Authenticator) null);
             ts = session.getTransport();
             ts.connect(mail.getHost(), mail.getSender(), mail.getPassword());
             Message msg = createMailContent(session, mail);
             if (msg != null) {
                 ts.sendMessage(msg, msg.getAllRecipients());
                 ret = true;
             }

             ts.close();
             ts = null;

             return ret;
         } catch (Exception e) {
             throw new RuntimeException(e.getMessage());
         } finally {
             if (null != ts) {
                 ts.close();
             }
         }
    }

    private static MimeMessage createMailContent(Session session, MailMsg mail) {
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(mail.getSender()));
            List<String> recs = StrTool.spilitString(mail.getReceiver(), ",");
            Iterator var4 = recs.iterator();

            while (var4.hasNext()) {
                String rec = (String) var4.next();
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(rec));
            }

            if (!StrTool.isBlankStr(mail.getCC())) {
                List<String> ccs = StrTool.spilitString(mail.getCC(), ",");
                Iterator var11 = ccs.iterator();

                while (var11.hasNext()) {
                    String cc = (String) var11.next();
                    message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
                }
            }

            message.setSubject(mail.getSubject());
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(mail.getMessage(), "text/html;charset=UTF-8");
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(text);
            if (SysTool.fileExists(mail.getAttachment()) == 1) {
                MimeBodyPart attach = new MimeBodyPart();
                DataHandler dh = new DataHandler(new FileDataSource(mail.getAttachment()));
                attach.setDataHandler(dh);
                attach.setFileName(dh.getName());
                mp.addBodyPart(attach);
            }

            mp.setSubType("mixed");
            message.setContent(mp);
            message.saveChanges();
            return message;
        } catch (Exception var8) {
            logger.error(var8, var8);
            return null;
        }
    }
}
